import {Component, ViewChild} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';
import {UpperWordCasePipe} from '../../pipes/upper-word-case/upper-word-case';
import {ValidateProvider} from "../../providers/validate/validate";
import {UtilsProvider} from "../../providers/utils/utils";
import {UrlProvider} from "../../providers/url/url";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ConfigProvider} from "../../providers/config/config";

@IonicPage({
    segment: 'service',
    defaultHistory: ['HomePage']
}) 
@Component({
    selector: 'page-service',
    templateUrl: 'service.html',
})
export class ServicePage {
    @ViewChild('layer') layer;
    navlists = [];
    banners = [];
    commends = [];
    operates = [];
    notices = [];
    msg = {};
    service_tel: any;
    
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public loadingCtrl: LoadingController,
        public upperWord: UpperWordCasePipe,
        public validate:ValidateProvider,
        public util: UtilsProvider,
        public url: UrlProvider,
        public httpService:HttpServicesProvider,
        public config: ConfigProvider
    ) {
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewDidLoad() {
        this.getData();
    }

    //跳转页面
    goPage(nav) {
        if (nav == null || nav.status != 1) {
            this.msg = {
                title: '程序员开发中',
                content: '<img src="assets/imgs/coding.jpg">'
            };
            this.layer.open = true;
            return false;
        }
        let params = {};
        if (nav.second_nav && nav.second_nav.length) {
            params = {
                nav_id: nav.key,
                sub_id: nav.second_nav[0].key,
                kind: nav.second_nav[0].kind
            };
        } else {
            params = {
                nav_id: nav.key,
                sub_id: nav.key,
                kind: nav.kind
            }
        }
        this.navCtrl.push(this.upperWord.transform(nav.kind) + 'Page', params);
    }

    getData() {
        let loading = this.util.loading();
        loading.show();
        //请求分类
        this.httpService.requestData('/service/get_nav',response => {
            this.navlists = response['data'];
        });

        this.httpService.requestData('/service/index', response => {
            loading.close();
            //全部数据
            let data = response['data'];
            //bannder
            this.banners = data['banners'];
            //commends 轮播
            this.commends = data['images'];
            //operates 操作指南
            this.operates = data['operate_list']['lists'];
            //notice 滚动文字信息
            this.notices = data['texts'];
            //客服电话：
            this.service_tel = data['service_tel'];

        });
    }

    //进入操作指南详情
    goDetail(amoy_id){
        this.navCtrl.push('OperationDetailPage',{
            id:amoy_id
        });
    }

    //跳转页面
    pageRedirect(page,data={}){
        this.navCtrl.push(page,data).then(success=>{
            if(success !== true){//页面需要登录后跳转
                this.navCtrl.push('LoginPage',data);
            }
        })
    }

    /**
     * 跳转到人才库
     * */
    goTalentPool(){
        let apiUrl = "/user_info/check_level";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken()
        }, (response)=>{
            if(response['error'] == 200 && response['reg_step'] == 2){
                this.navCtrl.push('TalentPoolSearchPage');
            }else{
                this.util.presentConfirm('您无权使用此功能，请完善注册资料；现在去完善？', ()=>{
                    this.navCtrl.push('RegisterPage');
                });
            }
        });
    }

}
